package com.ysqcould.web.controller.content;


import com.alibaba.fastjson.JSONObject;
import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.content.domain.Press;
import com.ysqcould.content.domain.PressType;
import com.ysqcould.content.domain.SecondType;
import com.ysqcould.content.domain.bo.PressBo;
import com.ysqcould.content.domain.vo.PressVo;
import com.ysqcould.content.mapper.PressTypeMapper;
import com.ysqcould.content.mapper.SecondTypeMapper;
import com.ysqcould.content.service.IPressService;
import com.ysqcould.system.domain.AssDict;
import com.ysqcould.system.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

import static com.ysqcould.allocation.utils.MultipartFileUtils.getFilePath;

/**
 * 新闻Controller
 * 
 * @author ruoyi
 * @date 2022-06-28
 */
@RestController
@RequestMapping("/content/press")
public class PressController extends BaseController
{
    @Autowired
    private IPressService pressService;

    @Resource
    private DictMapper dictMapper;

    @Resource
    private PressTypeMapper pressTypeMapper;

    @Resource
    private SecondTypeMapper secondTypeMapper;
    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('content:press:list')")
    @GetMapping("/list")
    public JSONObject list(Press press)
    {
        startPage();
        JSONObject json=new JSONObject();
        List<Press> list = pressService.selectPressList(press);
        final List<PressType> typeList = pressTypeMapper.selectAllType();
        final List<SecondType> secondTypeList = secondTypeMapper.selectSecondType();
        final TableDataInfo dataTable = getDataTable(list);
        json.put("typeList",typeList);
        json.put("secondTypeList",secondTypeList);
        json.put("data",dataTable);
        return json;
    }

    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('content:press:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Press press)
    {
        List<Press> list = pressService.selectPressList(press);
        ExcelUtil<Press> util = new ExcelUtil<Press>(Press.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:press:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
         final Press press = pressService.selectPressById(id);

        final List<PressType> allList = pressTypeMapper.selectPressTypeAllList(Long.parseLong(press.getType()));
        final List<SecondType> secondTypes = secondTypeMapper.selectByParentId(Long.parseLong(press.getType()));
        AssDict dict=new AssDict();
        //dict.setAsId(userId);
        dict.setTypeKey("article_type");
        final List<AssDict> assDictList = dictMapper.selectDictList(dict);
        ajax.put(AjaxResult.DATA_TAG,press);
        ajax.put("allList",allList);
        ajax.put("secondTypes",secondTypes);
        ajax.put("assDictList",assDictList);
        return  ajax;
    }

    /**
     * 新增时获取一级分类列表
     */
    @GetMapping("/getType")
    public AjaxResult getType(PressType pressType)
    {
        PressVo vo=new PressVo();
         Long userId = getUserId();
        Long id=null;
        if (userId==1){
            userId=id;
        }
        pressType.setAsId(userId);
        final List<PressType> types = pressTypeMapper.selectPressTypeList(pressType);
        AssDict assDict=new AssDict();
        assDict.setTypeKey("article_type");
        final List<AssDict> assDictList = dictMapper.selectDictList(assDict);
        vo.setTypeList(types);
        vo.setAssDictList(assDictList);
        return AjaxResult.success(vo);
    }



    /**
     * 获取二级分类列表
     */
    @PreAuthorize("@ss.hasPermi('content:press:query')")
    @GetMapping(value = "getSecondType/{val}")
    public AjaxResult getSecondType(@PathVariable("val") Long val)
    {
        System.out.println(val);
        return AjaxResult.success(pressService.selectPressSecondType(val));
    }
    /**
     * 下架文章
     * @param id 文章id
     */
    @PostMapping(value = "changStatus/{id}")
    public AjaxResult changStatus(@PathVariable("id") Long id)
    {
        System.out.println(id);
        return toAjax(pressService.changStatus(id));
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "文章封面上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        final JSONObject path = getFilePath(file);
        final String pictures = UploadFileUtil.AliOSSUpload((InputStream) path.get("in"), (String)path.get("name"));
        return AjaxResult.success("上传成功",pictures);
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('content:press:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Press press)
    {
        press.setAsId(getUserId());
        return toAjax(pressService.insertPress(press));
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('content:press:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Press press)
    {
        return toAjax(pressService.updatePress(press));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('content:press:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pressService.deletePressByIds(ids));
    }



    /**
     * 获取公众号文章内容
     * @param body
     * @return
     */
    @PostMapping("/getValue")
    public  AjaxResult getFiddler(@RequestBody String body) {
         String[] split = body.split("~~~");
        System.out.println("获取的cookie:"+split[0]);
        //获取文章链接
      List<String>UrlList=  pressService.getUrlList(split[1]);
     //使用文章链接获取点赞接口参数
        System.out.println(UrlList);
        List<JSONObject>valueList=pressService.getVauleList(UrlList,split[0]);
     //获取文章内容
     return  null;
    }

    @PutMapping("/addContent")
    public AjaxResult addContent(@RequestBody PressBo bo){
        System.out.println(bo);
        return AjaxResult.success(pressService.getContent(bo));
    }


}




